package com.example.demo.controller.data;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.bean.DataBean;
import com.example.demo.controller.TableController;
import com.example.demo.entity.Data;
import com.example.demo.enums.View;
import com.example.demo.exception.BusinessException;
import com.example.demo.exception.ErrorCode;
import com.example.demo.service.IDataService;
import com.example.demo.util.DateUtil;
import com.example.demo.util.NavUtil;
import de.felixroske.jfxsupport.FXMLController;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TextField;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author hweiyu
 * @Description
 * @Date 2022/6/23 12:12
 */
@FXMLController
public class DataIndexController extends TableController {

    @Autowired
    private IDataService dataService;

    @FXML
    private TextField qTopic;

    @Override
    public void refreshTable() {
        setPageNo(0);
        qTopic.setText(null);
        this.doSelect(null);
        getTable().refresh();
    }

    @Override
    public void doInit() {
        this.doSelect(null);
    }

    @Override
    public void doPageCallback() {
        this.doSelect(null);
    }

    @FXML
    public void doSelect(Event event) {
        Page<Data> p = new Page<>(getPageNo(), getPageSize());
        IPage<Data> data = dataService.page(p, Wrappers.<Data>lambdaQuery()
                .like(null != qTopic.getText() && !"".equals(qTopic.getText()), Data::getTopic, qTopic.getText()));
        List<DataBean> records = Optional.ofNullable(data.getRecords()).orElse(new ArrayList<>(0))
                .stream()
                .map(e -> {
                    DataBean bean = new DataBean();
                    bean.setId(String.valueOf(e.getId()));
                    bean.setTopic(e.getTopic());
                    bean.setContent(e.getContent());
                    bean.setRemark(e.getRemark());
                    bean.setCreateTime(DateUtil.format(e.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
                    bean.setUpdateTime(DateUtil.format(e.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
                    return bean;
                })
                .collect(Collectors.toList());
        renderPage(data.getTotal(), records);
    }

    @FXML
    public void toInsert(Event event) {
        NavUtil.toPage(View.DATA_INSERT, null);
    }

    @FXML
    public void toUpdate(Event event) {
        long count = getTable().getItems().stream()
                .filter(e -> ((DataBean) e).getChecked())
                .count();
        if (count == 0) {
            throw new BusinessException(ErrorCode.SELECT_DATA_ERROR);
        } else if (count > 1) {
            throw new BusinessException(ErrorCode.ONLY_ONE_DATA_ERROR);
        }
        String id = getTable().getItems().stream()
                .filter(e -> ((DataBean) e).getChecked())
                .map(e -> ((DataBean) e).getId())
                .findFirst()
                .orElse(null);
        NavUtil.toPage(View.DATA_UPDATE, id);
    }

    @FXML
    public void doDelete(Event event) {
        long count = getTable().getItems().stream()
                .filter(e -> ((DataBean) e).getChecked())
                .count();
        if (count == 0) {
            throw new BusinessException(ErrorCode.SELECT_DATA_ERROR);
        } else if (count > 1) {
            throw new BusinessException(ErrorCode.ONLY_ONE_DATA_ERROR);
        }
        String id = getTable().getItems().stream()
                .filter(e -> ((DataBean) e).getChecked())
                .map(e -> ((DataBean) e).getId())
                .findFirst()
                .orElse(null);

        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setContentText("确认删除吗？");
        alert.setHeaderText(null);
        alert.setTitle("提示");
        Optional<ButtonType> btn = alert.showAndWait();
        if (ButtonBar.ButtonData.OK_DONE == btn.get().getButtonData()) {
            dataService.removeById(id);
            this.refreshTable();
        }
    }

}
